import * as cc from "cc";
import { VMCustomAttr, VMEntity } from "../VMEntity";
import { VM } from "../VMMgr";

declare let CC_EDITOR;

export class VMCustomEntity extends VMEntity{
   
    observerAttr: VMCustomAttr;
    // 执行组件
    private _execComponent: cc.Component;
    public get execComponent(): cc.Component {
        if(!this._execComponent){
            if(this.observerAttr.execComponent){
                // @ts-ignore
                this._execComponent = this.component.getComponent(this.observerAttr.execComponent);
            }else{
                this._execComponent = this.component as any;
            }
        }
        return this._execComponent;
    }

    onBind() {
        if(CC_EDITOR){ return; }
        
    }
    onUnBind() {
        if(CC_EDITOR){ return; }
        
    }
    onValueInit() {
        if(CC_EDITOR){ return; }
        let _watchPath = this.observerAttr.watchPath;
        if(Array.isArray(_watchPath)){
            let max = _watchPath.length;
            for (let i = 0; i < max; i++) {
                let val = VM.getValue(_watchPath[i],null);
                this.templateValuesCache[i] = val;
            }
            this._updateValue(this.templateValuesCache,null,_watchPath); // 重新解析
        }else{
            let val = VM.getValue(_watchPath as string);
            this._updateValue(val,null,_watchPath);
        }
    }
    onStart() {
        if(CC_EDITOR){ return; }
        
    }
    

    protected onValueChanged(newValue: any, oldValue: any, pathArr: readonly string[]) {
        if(CC_EDITOR){ return; }
        let path = pathArr.join('.');
        let _watchPath = this.observerAttr.watchPath;
        let vmTween = this.observerAttr.vmTween as IVMTween;
        
        let _resolve = (_newValue: any,_oldValue: any,_path: any)=>{
           
            this._updateValue(_newValue,_oldValue,_path); // 重新解析
        }

        if(Array.isArray(_watchPath)){
            let _oldVal = [...this.templateValuesCache];
            //寻找缓存位置
            let index = _watchPath.findIndex(v => v === path);
            if (index >= 0) {
                //如果是所属的路径，就可以替换文本了
                this.templateValuesCache[index] = newValue; //缓存值
            }


            if(vmTween){
                vmTween.onTransition(this.templateValuesCache,_oldVal,_watchPath,_resolve);
            }else{
                _resolve(this.templateValuesCache,_oldVal,_watchPath);
            }

        }else{
            
            if(vmTween){
                vmTween.onTransition(newValue,oldValue,_watchPath,_resolve);
            }else{
                _resolve(newValue,oldValue,_watchPath);
            }
        }
    }

    protected _updateValue(newValue,oldValue,watchPath){
        let val = this.formatValue(newValue,oldValue,this.node,0,watchPath);
        this.execComponent[this._property] = val;
    }
    
    get _property(){
        let _observerAttr = this.observerAttr;
        return _observerAttr.property;
    }
    onCheckProperty(){
        return this._property in this.execComponent;
    }
}

